import { useQuery, useInfiniteQuery } from '@tanstack/vue-query'
import {
  getTopicDetail,
  getClassList,
  getTopicList,
  getHotTopicList,
  fetchTopicMember,
  fetchTopicMemberBlacklistList,
  getDiscussList,
  getHotDiscuss,
  fetchTopicJoinVerify,
  fetchTopicJoinVerifyList,
} from '@/axios/api/topic'
import { isLastPage } from '@/utils/dataUtils'

export function useTopicDataQuery(id) {
  return useQuery({
    queryKey: ['topicDetail', id],
    queryFn: getTopicDetail,
    enabled: !!id,
  })
}

// 获取主题分类列表
export function useTopicClassListQuery() {
  return useQuery({
    queryKey: ['topicClassList'],
    queryFn: getClassList,
  })
}

// 获取主题列表
export function useTopicListQuery(classId) {
  return useInfiniteQuery({
    queryKey: ['topicList', classId],
    queryFn: getTopicList,
    getNextPageParam: (lastPage) => {
      if (isLastPage(lastPage)) {
        return undefined
      }
      return lastPage.result.current_page + 1
    },
  })
}

// 获取热门主题列表
export function useHotTopicListQuery(id) {
  return useInfiniteQuery({
    queryKey: ['hotTopicList', id],
    queryFn: getHotTopicList,
    getNextPageParam: (lastPage) => {
      if (isLastPage(lastPage)) {
        return undefined
      }
      return lastPage.result.current_page + 1
    },
  })
}

// 获取主题成员
export function useTopicMemberQuery(id) {
  return useInfiniteQuery({
    queryKey: ['topicMember', id],
    queryFn: fetchTopicMember,
    getNextPageParam: (lastPage) => {
      if (isLastPage(lastPage)) {
        return undefined
      }
      return lastPage.result.current_page + 1
    },
    enabled: !!id,
  })
}

// 获取黑名单列表
export function useTopicMemberBlacklistListQuery(id) {
  return useInfiniteQuery({
    queryKey: ['topicMemberBlacklistList', id],
    queryFn: fetchTopicMemberBlacklistList,
    getNextPageParam: (lastPage) => {
      if (isLastPage(lastPage)) {
        return undefined
      }
      return lastPage.result.current_page + 1
    },
    enabled: !!id,
  })
}

// 获取话题列表
export function useDiscussListQuery(id) {
  return useInfiniteQuery({
    queryKey: ['discussList', id],
    queryFn: getDiscussList,
    getNextPageParam: (lastPage) => {
      if (isLastPage(lastPage)) {
        return undefined
      }
      return lastPage.result.current_page + 1
    },
  })
}

// 获取热门话题
export function useHotDiscussQuery() {
  return useQuery({
    queryKey: ['hotDiscuss'],
    queryFn: getHotDiscuss,
  })
}

// 获取主题管理员获取进圈验证
export function useTopicJoinVerifyQuery(id) {
  return useQuery({
    queryKey: ['topicJoinVerify', id],
    queryFn: fetchTopicJoinVerify,
  })
}

// 获取进圈验证列表
export function useTopicJoinVerifyListQuery(id, type) {
  return useInfiniteQuery({
    queryKey: ['topicJoinVerifyList', id, type],
    queryFn: fetchTopicJoinVerifyList,
    getNextPageParam: (lastPage) => {
      if (isLastPage(lastPage)) {
        return undefined
      }
      return lastPage.result.current_page + 1
    },
  })
}
